<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="https://api.vercel.com/now/files/57dbce39c99b5c73e5c7f933a3d4bf1694fd1e41999e4d9f08a96a87733f5940/LogoMakr-6bamev.png"/>
	<link rel="shortcut icon" href="https://api.vercel.com/now/files/57dbce39c99b5c73e5c7f933a3d4bf1694fd1e41999e4d9f08a96a87733f5940/LogoMakr-6bamev.png">
	
			    <title>
    Fly's Blog
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/css/mic_main.css" />
    <link rel="stylesheet" href="/css/dropdownMenu.css" />
    <meta name="keywords" content="miccall" />
    <script data-ad-client="ca-pub-4558202898504715" async     src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/js/jquery.min.js"></script>
    <script src="/js/jquery.scrollex.min.js"></script>
    <script src="/js/jquery.scrolly.min.js"></script>
    <script src="/js/skel.min.js"></script>
    <script src="/js/util.js"></script>
    <script src="/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/" class="logo">Fly's Blog</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/categories/Welcome/">Welcome</a></li><li><a class="category-link" href="/categories/hexo/">hexo</a></li><li><a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/">学习</a></li><li><a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/NLP/">NLP</a></li><li><a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/git/">git</a></li><li><a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/%E5%8D%9A%E5%AE%A2/">博客</a></li><li><a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/%E5%8D%9A%E5%AE%A2/Netch/">Netch</a></li><li><a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/%E5%8D%9A%E5%AE%A2/VUE/">VUE</a></li><li><a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1/">数学建模</a></li><li><a class="category-link" href="/categories/%E6%B1%87%E7%BC%96/">汇编</a></li><li><a class="category-link" href="/categories/%E7%BD%91%E7%9B%98/">网盘</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/yu2256140203" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://i.loli.net/2020/07/31/MvYaBCwx1Aont9F.png#vwid=1397&amp;vhei=698);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >LSTM使用MNIST手写数字识别实战的代码和心得</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <blockquote>
<p>RNN的架构除了RNN类中的模型不同，其他的构架与CNN类似，如果还没有阅读过CNN文章的可以点击下方链接进入：<br><a target="_blank" rel="noopener" href="https://void.fly-kiss.xyz/index.php/archives/7/">CNN使用MNIST手写数字识别实战的代码和心得</a><br>LSTM(Long Short-Term Memory长短时记忆网络)虽然在MNIST手写数字识别方面不擅长，但是也可以进行使用，效果比CNN略显逊色</p>
</blockquote>
<p>对LSTM使用MNIST手写数字识别的思路图<br><img src="https://i.loli.net/2020/07/31/MvYaBCwx1Aont9F.png#vwid=1397&vhei=698" alt="LSTM进行MNIST手写数字识别"><br>LSTM是在RNN的主线基础上增加了支线，增加了三个门，输入门，输出门和忘记门。<br>避免了可能因为加权问题，使程序忘记之前的内容，梯度弥散或者梯度爆炸。<br>batch_size在这里选取的是100，选择了一个隐藏层和128的神经元，对LSTM结构进行部署，<br>MNIST长宽为28，选取一行28作为一份数据传入input_size,RNN是按照时间序列进行传值，batch_size为100，也就是在每次传入的数据为（128，28）<br>进入隐藏层后，out结果张量的shape为（[100, 28, 128]）<br>在out[:, -1, :]时间序列中取得最后一次的输出，得到（[100,  128]）<br>再进入全连接层后将hidden_size的128变为所需要的输出的10种图片的维度（[100,  10]）</p>
<p>对超参数的定义<br>    #定义超参数<br>    input_size = 28<br>    time_step = 28# 时间序列<br>    Layers = 1# 隐藏单元的个数<br>    hidden_size = 128# 每个隐藏单元中神经元个数<br>    classes = 10<br>    batch_size = 100<br>    EPOCHS = 10<br>    learning_rate = 0.01 #学习率<br>RNN对于数据的读取有别于CNN,按照时间来读取，在这里可以将input_size看作是图片的长，而time_step看作宽的长度。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#Long Short-Term Memory(长短时记忆网络)</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RNN</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, input_size, hidden_size, Layers, classes</span>):</span></span><br><span class="line">        <span class="built_in">super</span>(RNN, self).__init__()</span><br><span class="line">        self.Layers = Layers</span><br><span class="line">        self.hidden_size = hidden_size</span><br><span class="line">        self.lstm = nn.LSTM(input_size, hidden_size, Layers, batch_first=<span class="literal">True</span>)</span><br><span class="line">        self.fc = nn.Linear(hidden_size, classes)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, x</span>):</span></span><br><span class="line">        <span class="comment"># 设置初始隐藏状态和单元格状态</span></span><br><span class="line">        h0 = torch.zeros(self.Layers, x.size(<span class="number">0</span>), self.hidden_size).to(device)</span><br><span class="line">        c0 = torch.zeros(self.Layers, x.size(<span class="number">0</span>), self.hidden_size).to(device)</span><br><span class="line">        <span class="comment"># out张量的shape(batch_size, time_step, hidden_size)</span></span><br><span class="line">        out, _ = self.lstm(x, (h0, c0))<span class="comment">#torch.Size([100, 28, 128])</span></span><br><span class="line">        <span class="comment">#out[:, -1, :].shape torch.Size([100, 128])</span></span><br><span class="line">        <span class="comment"># 只得到时间顺序点的最后一步</span></span><br><span class="line">        out = self.fc(out[:, -<span class="number">1</span>, :])<span class="comment">#torch.Size([100, 10])</span></span><br><span class="line">        <span class="keyword">return</span> out</span><br></pre></td></tr></table></figure>
<p>运行结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">RNN(</span><br><span class="line">  (lstm): LSTM(<span class="number">28</span>, <span class="number">128</span>, batch_first=<span class="literal">True</span>)</span><br><span class="line">  (fc): Linear(in_features=<span class="number">128</span>, out_features=<span class="number">10</span>, bias=<span class="literal">True</span>)</span><br><span class="line">)</span><br><span class="line">Epoch [<span class="number">10</span>/<span class="number">10</span>],  Loss: <span class="number">0.0115</span></span><br><span class="line">Test Accuracy to test: <span class="number">98.07</span> %</span><br></pre></td></tr></table></figure>

            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'http://example.com/2021/03/04/LSTM%E4%BD%BF%E7%94%A8MNIST%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB%E5%AE%9E%E6%88%98%E7%9A%84%E4%BB%A3%E7%A0%81%E5%92%8C%E5%BF%83%E5%BE%97/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'http://example.com/2021/03/04/LSTM%E4%BD%BF%E7%94%A8MNIST%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB%E5%AE%9E%E6%88%98%E7%9A%84%E4%BB%A3%E7%A0%81%E5%92%8C%E5%BF%83%E5%BE%97/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
       <li>「Powered by <a data-from="10680" href="https://webify.cloudbase.net/" target="_blank" rel="nofollow noopener noreferrer">CloudBase Webify</a>」</li>
        </div>
    </div>
</body>



 	
</html>
